package com.qc31.monitorlib.ui.carreport

import android.view.View
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.qc31.baselib.base.LazyFragment
import com.qc31.baselib.interfaces.SimpleTabLayoutListener
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.FragmentCarReportBinding

/** @Author Jasper @Time 2020/10/20 @Desc 报表 */
class CarReportFragment :
    LazyFragment<FragmentCarReportBinding>(FragmentCarReportBinding::inflate) {

    override fun initView(view: View) {


    }

    override fun initData() {
        initFragment()
        initTabLayout()
    }

    override fun initEvent() {

    }

    private val fragments = ArrayList<Fragment>()
    private fun initFragment() {
        fragments.add(ReportItemFragment(0))
        fragments.add(ReportAlarmFragment())
        fragments.add(ReportAlarmFragment(true))
        fragments.add(ReportItemFragment(3))
        fragments.add(CarReportFeeFragment())
        showFragment(fragments[0])
    }

    private var currentFragment: Fragment = Fragment()

    /** 显示Fragment */
    private fun showFragment(fragment: Fragment) {
        if (currentFragment == fragment) return
        childFragmentManager.beginTransaction().apply {
            hide(currentFragment)
            currentFragment = fragment
            if (fragment.isAdded) {
                show(fragment)
            } else {
                add(R.id.flCarContent, fragment).show(fragment)
            }
        }.commit()
    }

    private fun initTabLayout() {
        val titles: Array<String> = resources.getStringArray(R.array.car_report_title)
        for ((index, title) in titles.withIndex()) {
            binding.tabLayout.addTab(binding.tabLayout.newTab())
            binding.tabLayout.getTabAt(index)?.text = title
        }
        binding.tabLayout.addOnTabSelectedListener(object : SimpleTabLayoutListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val position = tab?.position ?: 0
//                if (position == 1 || position == 2) {
//                    binding. ivReportSet.visibility = View.VISIBLE
//                } else {
//                    binding.ivReportSet.visibility = View.GONE
//                }
                showFragment(fragments[position])
            }
        })
    }
}